/*
 * @Author: 朱自强
 * @Date: 2023-11-09 11:32:59
 * @LastEditors: 朱自强
 * @LastEditTime: 2023-11-09 14:21:03
 * @FilePath: \vite-project\src\request\index.ts
 * @Description:
 *
 * Copyright (c) 2023 by 朱自强, All Rights Reserved.
 */
//在request文件下的index.ts配置

import axios from "axios";

export function request(config: any) {
  const baseURL = process.env.APP_BASE_URL;
  const instance = axios.create({
    baseURL: baseURL,
    timeout: 20000,
    headers: {
      "Content-Type": "application/json;charset=utf-8",
    },
  });

  //请求拦截器
  instance.interceptors.request.use(
    (config) => {
      if (localStorage.getItem("token")) {
        config.headers.Authorization = localStorage.getItem("token");
      }
      return config;
    },
    (err) => {
      return Promise.reject(err);
    }
  );

  // 响应拦截器
  instance.interceptors.response.use(
    (res) => {
      return res.data;
    },
    (err) => {
      return Promise.reject(err);
    }
  );
  return instance(config);
}
